package com.echoesnet.echoesshortvideo.views.adapters;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.echoesnet.echoesshortvideo.AppController;
import com.echoesnet.echoesshortvideo.Beans.CommentListBean;
import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.Image.GlideApp;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

public class CommentListAdapter extends UltimateViewAdapter<UltimateRecyclerviewViewHolder>
{
    private String TAG = CommentListAdapter.class.getSimpleName();

    private static final int VIEW_TYPE_VIDEO = 1000;
    private Activity mAct;
    private List<CommentListBean.CommentRowsInfo> dataList;

    private int itemW, itemH;

    public CommentListAdapter(Activity act, List<CommentListBean.CommentRowsInfo> list)
    {
        this.mAct = act;
        this.dataList = list;
//        itemW = (CommonUtils.getScreenWidth(mAct)-CommonUtils.dp2px(mAct, 1)) / 2;
//        itemW = CommonUtils.getScreenWidth(mAct) / 2;
//        //<!-- 374px * 480 px -->
//        //<!-- 高是宽的1.28倍 -->
//        itemH = (int) (itemW * 1.28);
    }

    // 复写 UltimateViewAdapter 增加自己类型------------------------------------------------------
    @Override
    public UltimateRecyclerviewViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        if (VIEW_TYPE_VIDEO == viewType)
        {
            View view = LayoutInflater.from(mAct).inflate(R.layout.item_comment_list, parent, false);
//            view.setLayoutParams(new ViewGroup.LayoutParams(itemW, itemH));

            view.findViewById(R.id.commentUserHeader).setOnClickListener(v -> {
                if (onItemClickListener != null)
                {
                    onItemClickListener.onClick(v, (Integer) v.getTag(R.id.viewTagPosition), (CommentListBean.CommentRowsInfo) v.getTag(R.id.viewTagFirst));
                }
            });
            view.findViewById(R.id.llLike).setOnClickListener(v -> {
                if (onItemClickListener != null)
                {
                    onItemClickListener.onClick(v, (Integer) v.getTag(R.id.viewTagPosition), (CommentListBean.CommentRowsInfo) v.getTag(R.id.viewTagSecond));
                }
            });

            view.findViewById(R.id.llCommentContent).setOnClickListener(v -> {
                if (onItemClickListener != null)
                {
                    onItemClickListener.onClick(v, (Integer) v.getTag(R.id.viewTagPosition), (CommentListBean.CommentRowsInfo) v.getTag(R.id.viewTagSecond));
                }
            });

            view.findViewById(R.id.llCommentContent).setOnLongClickListener(v -> {
                if (onItemLongClickListener != null)
                {
                    onItemLongClickListener.onLongClick(v, (Integer) v.getTag(R.id.viewTagPosition), (CommentListBean.CommentRowsInfo) v.getTag(R.id.viewTagSecond));
                }
                return false;
            });
            return new UltimateRecyclerviewViewHolder(view);
        }
        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public int getItemViewType(int position)
    {
        if (VIEW_TYPES.NORMAL != super.getItemViewType(position))
        {
            return super.getItemViewType(position);
        }
        else
        {
//            int fixPosition = position + (getItemCount() - getAdapterItemCount()); // 当前位置 补偿 总item与内容item的差值
//            if(enableLoadMore())
//                fixPosition -=1;    //footer 存在 刚才就加多了

            return VIEW_TYPE_VIDEO;
        }
    }

    // 无用
    @Override
    public UltimateRecyclerviewViewHolder onCreateViewHolder(ViewGroup parent)
    {
        return null;
    }
    // END----------------- 复写 UltimateViewAdapter 增加自己类型------------------------------------------------------


    @Override
    public void onBindViewHolder(UltimateRecyclerviewViewHolder viewHolder, int position)
    {
        if (getItemViewType(position) == VIEW_TYPE_VIDEO)
        {
            bindDefaultTileItem(position, viewHolder);
        }
//        else if (getItemViewType(position) == VIEW_TYPE_GALLERY) {
//            final GalleryViewHolder holder = (GalleryViewHolder) viewHolder;
//            bindLoadingGallery(holder);
//        }
    }


    private void bindDefaultTileItem(int position, UltimateRecyclerviewViewHolder holder)
    {
        CommentListBean.CommentRowsInfo commentInfo = dataList.get(position);

        TextView tvCommentNickName = holder.getView().findViewById(R.id.commentNickName);
        TextView tvCommentReplyNickName = holder.getView().findViewById(R.id.commentReplyNickName);
        TextView tvCommentContent = holder.getView().findViewById(R.id.commentContent);
        TextView tvCommentReplyContent = holder.getView().findViewById(R.id.commentReplyContent);
        TextView tvCommentDate = holder.getView().findViewById(R.id.commentDate);
        TextView tvLikeNum = holder.getView().findViewById(R.id.tvCommentLikeNum);

        LinearLayout llCommentContent = holder.getView().findViewById(R.id.llCommentContent);
        LinearLayout rlCommentReply = holder.getView().findViewById(R.id.rlCommentReply);
        LinearLayout deleteCommentContent = holder.getView().findViewById(R.id.deleteCommentContent);
        LinearLayout llLike = holder.getView().findViewById(R.id.llLike);


        ImageView ivLike = holder.getView().findViewById(R.id.ivLike);
        ImageView ivAvatar = holder.getView().findViewById(R.id.commentUserHeader);


        String nicName, commentContent, commentReplyNickName = "", commentReplyContent = "";

        if (commentInfo.getReplys() != null && !TextUtils.isEmpty(commentInfo.getReplys().getCommentId()))
        {
            rlCommentReply.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(commentInfo.getComment().getNickname()))
                nicName = commentInfo.getComment().getNickname();
            else
                nicName = commentInfo.getComment().getUserId();
            if (!TextUtils.isEmpty(commentInfo.getReplys().getNickname()))
                commentReplyNickName = commentInfo.getReplys().getNickname();
            else
                commentReplyNickName = commentInfo.getReplys().getUserId();

            commentContent = commentInfo.getComment().getComment();
            commentReplyContent = commentInfo.getReplys().getComment();
        }
        else
        {
            if (!TextUtils.isEmpty(commentInfo.getComment().getNickname()))
                nicName = commentInfo.getComment().getNickname();
            else
                nicName = commentInfo.getComment().getUserId();
            commentContent = commentInfo.getComment().getComment();

            if ("1".equals(commentInfo.getReplys().getDelStatus()))
            {
                deleteCommentContent.setVisibility(View.VISIBLE);
                rlCommentReply.setVisibility(View.GONE);
            }
            else
            {
                deleteCommentContent.setVisibility(View.GONE);
                rlCommentReply.setVisibility(View.GONE);
            }
        }
        tvCommentNickName.setText(nicName);
        tvCommentContent.setText(commentContent);
        tvCommentDate.setText(commentInfo.getComment().getTimeToNow());
        tvLikeNum.setText(commentInfo.getComment().getLikeNum());
        ivLike.setImageResource("0".equals(commentInfo.getComment().getLikeOrNot()) ?
                R.drawable.but_small_heart_default : R.drawable.but_small_heart_activate);
        GlideApp.with(AppController.getInstance())
                .load(commentInfo.getComment().getHeadPic())
                .centerCrop()
                .circleCrop()
                .error(R.drawable.default_80px)
                .into(ivAvatar);

        tvCommentReplyNickName.setText(commentReplyNickName);
        tvCommentReplyContent.setText(commentReplyContent);
//        //给listen加值，在 onCreateViewHolder 里；
        ivAvatar.setTag(R.id.viewTagFirst, commentInfo);
        ivAvatar.setTag(R.id.viewTagPosition, position);
        llLike.setTag(R.id.viewTagSecond, commentInfo);
        llLike.setTag(R.id.viewTagPosition, position);
        llCommentContent.setTag(R.id.viewTagSecond, commentInfo);
        llCommentContent.setTag(R.id.viewTagPosition, position);

    }


    @Override
    public long getItemId(int position)
    {
        return position;
    }


    private ItemClickListener onItemClickListener;

    public void setOnItemClickListener(ItemClickListener l)
    {
        this.onItemClickListener = l;
    }

    public interface ItemClickListener
    {
        void onClick(View v, int position, CommentListBean.CommentRowsInfo tag);
    }

    private ItemLongClickListener onItemLongClickListener;

    public void setOnItemLongClickListener(ItemLongClickListener l)
    {
        this.onItemLongClickListener = l;
    }

    public interface ItemLongClickListener
    {
        void onLongClick(View v, int position, CommentListBean.CommentRowsInfo tag);
    }


    @Override
    public UltimateRecyclerviewViewHolder newFooterHolder(View view)
    {
        return null;
    }

    @Override
    public UltimateRecyclerviewViewHolder newHeaderHolder(View view)
    {
        return null;
    }


    @Override
    public int getAdapterItemCount()
    {
        return dataList.size();
    }

    @Override
    public long generateHeaderId(int position)
    {
        return 0;
    }


    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent)
    {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position)
    {

    }


}
